#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include "pokedex.h"

using namespace std;

PokedexClass::PokedexClass(QWidget *parent) : QWidget(parent)
{
	setupUi(this);
	image->setPixmap(QPixmap("Pokeball.png"));
	curentId = 0;
	competMemo = 0;
}

vector<Pokemon *>	&	PokedexClass::getListPokemon()
{
	return (this->_listPokemon);
}

map<string, Skill *> &	PokedexClass::getListSkills()
{
	return (this->_listSkills);
}

void PokedexClass::clearLabel()
{
	labTitre1->setText("");
	labTitre2->setText("");
	labTitre3->setText("");
	labTitre4->setText("");
	labTitre5->setText("");
	labTitre6->setText("");
	labContent1->setText("");
	labContent2->setText("");
	labContent3->setText("");
	labContent4->setText("");
	labContent5->setText("");
	labContent6->setText("");
}

void PokedexClass::on_butProfil_clicked()
{
	std::ostringstream	oss;
	std::string			result;
	std::ostringstream	type;
	std::ostringstream	imageFile;
	vector<string>::const_iterator it = getListPokemon()[curentId]->getTypes().begin();
	vector<string>::const_iterator coma = getListPokemon()[curentId]->getTypes().begin();
	vector<string>::const_iterator ite = getListPokemon()[curentId]->getTypes().end();

	imageFile << "PokemonImages/";
	if (curentId + 1 < 100)
		imageFile << "0";
	if (curentId + 1 < 10)
		imageFile << "0";
	imageFile << curentId + 1;
	imageFile << ".png";
	QImage				go_image(imageFile.str().c_str());
	image->setPixmap(QPixmap::fromImage(go_image.scaled(439, 309, Qt::KeepAspectRatio)));
	labType->setText("Profil : ");
	clearLabel();

	/* ID */
	labTitre1->setText("Numero : ");
	oss << curentId + 1;
    result = oss.str();
	labContent1->setText(result.c_str());

	/* name */
	labTitre2->setText("Nom : ");
	labContent2->setText(getListPokemon()[curentId]->getName().c_str());

	/* type */
	labTitre3->setText("Type : ");
	while (it != ite)
	{
		if (it != coma)
			type << " ,";
		type << *it;
		++it;
	}
	result = type.str();
	labContent3->setText(result.c_str());

	/* taille */
	oss.str("");
	oss << fixed << setprecision(2) << getListPokemon()[curentId]->getHeight();
	oss << " m";
    result = oss.str();
	labTitre4->setText("Taille : ");
	labContent4->setText(result.c_str());

	/* poids */
	oss.str("");
	oss << fixed << setprecision(2) << getListPokemon()[curentId]->getWeight();
	oss << " kg";
    result = oss.str();
	labTitre5->setText("Poids : ");
	labContent5->setText(result.c_str());

	/* description */
	labTitre6->setText("Description : ");
	labContent6->setText(getListPokemon()[curentId]->getDescription().c_str());
}

void PokedexClass::on_butStats_clicked()
{
	std::ostringstream	oss;
	std::string			result;
	labType->setText("Statistiques : ");
	clearLabel();

	oss << getListPokemon()[curentId]->getStats().getHP();
    result = oss.str();
	labTitre1->setText("HP : ");
	labContent1->setText(result.c_str());

	oss.str("");
	oss << getListPokemon()[curentId]->getStats().getAtk();
    result = oss.str();
	labTitre2->setText("ATK : ");
	labContent2->setText(result.c_str());

	oss.str("");
	oss << getListPokemon()[curentId]->getStats().getDef();
    result = oss.str();
	labTitre3->setText("DEF : ");
	labContent3->setText(result.c_str());

	oss.str("");
	oss << getListPokemon()[curentId]->getStats().getSpd();
    result = oss.str();
	labTitre4->setText("SPD : ");
	labContent4->setText(result.c_str());

	oss.str("");
	oss << getListPokemon()[curentId]->getStats().getSat();
    result = oss.str();
	labTitre5->setText("SAT : ");
	labContent5->setText(result.c_str());

	oss.str("");
	oss << getListPokemon()[curentId]->getStats().getSdf();
    result = oss.str();
	labTitre6->setText("SDF : ");
	labContent6->setText(result.c_str());
}

void PokedexClass::on_butEvolutions_clicked()
{
	std::ostringstream	oss;
	std::string			result;
	vector<pair<string, int>>::const_iterator it = getListPokemon()[curentId]->getEvolutions().begin();
	vector<pair<string, int>>::const_iterator ite = getListPokemon()[curentId]->getEvolutions().end();

	labType->setText("Evolutions : ");
	clearLabel();

	if (it != ite)
	{
		labTitre1->setText("Evolution 1 : ");
		labContent1->setText(getListPokemon()[curentId]->getEvolutions()[0].first.c_str());
		oss << getListPokemon()[curentId]->getEvolutions()[0].second;
		result = oss.str();
		labTitre2->setText("lvl requis: ");
		labContent2->setText(result.c_str());
		++it;
		if (it != ite)
		{
			labTitre3->setText("Evolution 2 : ");
			labContent3->setText(getListPokemon()[curentId]->getEvolutions()[1].first.c_str());
			oss.str("");
			oss << getListPokemon()[curentId]->getEvolutions()[1].second;
			result = oss.str();
			if (result == "1")
				result = "Inconnu";
			labTitre4->setText("lvl requis: ");
			labContent4->setText(result.c_str());
			++it;
			if (it != ite)
			{
				labTitre5->setText("Evolution 3 : ");
				labContent5->setText(getListPokemon()[curentId]->getEvolutions()[2].first.c_str());
				oss.str("");
				oss << getListPokemon()[curentId]->getEvolutions()[2].second;
				result = oss.str();
				if (result == "1")
					result = "Inconnu";
				labTitre6->setText("lvl requis: ");
				labContent6->setText(result.c_str());
			}
		}
	}
}

void PokedexClass::on_butRatio_clicked()
{
	std::ostringstream	oss;
	std::string			result;

	labType->setText("Ratio : ");
	clearLabel();

	oss << fixed << setprecision(2) << getListPokemon()[curentId]->getRatio().first;
	oss << " %";
    result = oss.str();
	labTitre1->setText("Male : ");
	labContent1->setText(result.c_str());

	oss.str("");
	oss << fixed << setprecision(2) << getListPokemon()[curentId]->getRatio().second;
	oss << " %";
    result = oss.str();
	labTitre2->setText("Femelle : ");
	labContent2->setText(result.c_str());
}

void PokedexClass::on_butCompet_clicked()
{
	static int										memo;
	std::ostringstream								oss;
	std::string										result;
	vector<pair<string, int>>::const_iterator it =	getListPokemon()[curentId]->getSkills().begin();
	vector<pair<string, int>>::const_iterator ite =	getListPokemon()[curentId]->getSkills().end();

	/*while (it != ite)
	{
		cout << "Skills: " << it->first  << "lvl:" << it->second << endl;
		++it;
	}*/
	labType->setText("Competence : ");
	clearLabel();
	if (competMemo == 0)
		memo = 0;
	competMemo = 0;
	it += memo;
	if (it != ite)
	{
		labTitre1->setText("Type : ");
		labContent1->setText(getListPokemon()[curentId]->getSkills()[memo].first.c_str());
		labTitre2->setText("Nom : ");
		labContent2->setText(getListPokemon()[curentId]->getSkills()[memo].first.c_str());
		oss << getListPokemon()[curentId]->getSkills()[memo].second;
		result = oss.str();
		labTitre3->setText("level : ");
		labContent3->setText(result.c_str());
		++memo;
	}
	else
		memo = 0;
}

void PokedexClass::on_recherche_clicked()
{
	int		id;
	int ite = getListPokemon().size();

	if (rechName->text() != "")
	{
		id = 0;
		while (id + 7 < ite)
		{
			if (rechName->text() == getListPokemon()[id]->getName().c_str())
			{
				curentId = id;
				id = ite + 1;
			}
			++id;
		}
	}
	else if (rechNum->value() + 6 < getListPokemon().size() && rechNum->value() > 0)
		curentId = rechNum->value() - 1;
	on_butProfil_clicked();
}

void PokedexClass::on_next_clicked()
{
	competMemo = 1;
	on_butCompet_clicked();
}